import Vue from 'vue'
import Vuex from "vuex"
import { addCount } from './mutations_type'

//1 安装插件
Vue.use(Vuex)


//创建对象
const store=new Vuex.Store({
    state:{
        counter:1000,
        students:[
            {id:110,name:"A",age:32},
            {id:110,name:"B",age:33},
            {id:110,name:"C",age:44}

        ],
        info:{
            name:"a",
            age :43,
            height:1.98
        }
    },
    mutations:{
        editInfo(state){
                Vue.set(state.info,'address',"洛杉矶")
        },
        //方法
        increment(state){
            state.counter++
        },
        decrement(state){
            state.counter--
        },
        [addCount](state,payload){
            console.log("state",state,payload)
            console.log("this.state",this.state)
            state.counter+=payload
        }
    },
    //backed api
    actions:{
        editInfo(context,payload){
           return new Promise((r,e)=>{
            setTimeout(() => {
                context.commit('editInfo')
                console.log(payload)
            }, 1000);
            r("done")
           })
         },
    },
    getters:{
        more20Stu(state){
            return  state.students.filter(s=>s.age>20)
        },
        //携带参数
        more20StuLength(state,getters){
            return  getters.more20Stu.length
        },
        more20StuParams(state){
            return function(age){
                    return state.students.filter(s=>s.age>age)
            }
        }
    },
    modules:{
        a: moduleA
    }
})

//模块化开发
const moduleA={
    state:{
        name: "ssd"
    },
    getters:{
        //使用上层模块里面的内容
        fullName(state,getters,rootState){
        return rootState.counter
        }
    },
    mutations:{

    },
    actions:{

    }
}
const obj={
    name3:'why',
    age:23,
    height:1.88
}


//导出store对象
export default store